package com.ruoyi.dl.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.dl.domain.DlUser;
import com.ruoyi.dl.service.IDlUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 前台用户Controller
 * 
 * @author ruoyi
 * @date 2021-01-05
 */
@Controller
@RequestMapping("/dl/user")
public class DlUserController extends BaseController
{
    private String prefix = "dl/user";

    @Autowired
    private IDlUserService dlUserService;

    @RequiresPermissions("dl:user:view")
    @GetMapping()
    public String user()
    {
        return prefix + "/user";
    }

    /**
     * 查询前台用户列表
     */
    @RequiresPermissions("dl:user:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DlUser dlUser)
    {
        startPage();
        List<DlUser> list = dlUserService.selectDlUserList(dlUser);
        return getDataTable(list);
    }

    /**
     * 导出前台用户列表
     */
    @RequiresPermissions("dl:user:export")
    @Log(title = "前台用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DlUser dlUser)
    {
        List<DlUser> list = dlUserService.selectDlUserList(dlUser);
        ExcelUtil<DlUser> util = new ExcelUtil<DlUser>(DlUser.class);
        return util.exportExcel(list, "user");
    }

    /**
     * 新增前台用户
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存前台用户
     */
    @RequiresPermissions("dl:user:add")
    @Log(title = "前台用户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DlUser dlUser)
    {
        return toAjax(dlUserService.insertDlUser(dlUser));
    }

    /**
     * 修改前台用户
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        DlUser dlUser = dlUserService.selectDlUserById(id);
        mmap.put("dlUser", dlUser);
        return prefix + "/edit";
    }

    /**
     * 修改保存前台用户
     */
    @RequiresPermissions("dl:user:edit")
    @Log(title = "前台用户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DlUser dlUser)
    {
        return toAjax(dlUserService.updateDlUser(dlUser));
    }

    /**
     * 删除前台用户
     */
    @RequiresPermissions("dl:user:remove")
    @Log(title = "前台用户", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(dlUserService.deleteDlUserByIds(ids));
    }

    //根据userId查询用户信息
    @PostMapping("/selectUserById")
    @ResponseBody
    public AjaxResult selectActivityById(Integer userId) {
        return AjaxResult.success("成功", dlUserService.selectDlUserById(userId));
    }
}
